﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace Billing.WindowsBase
{
    public partial class FormBase : Form, Billing.IMenuItem
    {
        public FormBase()
        {
            InitializeComponent();
        }
        public FormBase(bool Register)
        {
            this.register = Register;
            InitializeComponent();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            if (!this.DesignMode && this.Register)
            {
                Billing.FormRegister.UnregisterForm(this.GetType().FullName);
            }
        }

        protected override void OnLoad(EventArgs e)
        {


            if (Register)
            {
                new UserRightsManager().SetupProperties(this);
            }

            base.OnLoad(e);

            OnRegister();

        }

        protected virtual void OnRegister()
        {
            if (!this.DesignMode && this.Register)
            {
                Billing.FormRegister.RegisterForm(this, this.GetType().FullName);
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            if (this.CheckSaveOnClose)
                if (MessageBox.Show(this.MessageOnClose, "Billing", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.No)
                {
                    FormManager.AllowQuit = false;
                    e.Cancel = true;
                    return;
                }
        }
        #region IMenuItem Members
        bool register = true;
        bool isMDIChilde = true;
        [Category("Custom")]
        public bool IsMDIChilde
        {
            get
            { return this.isMDIChilde; }
            set
            { this.isMDIChilde = value; }
        }
        [Category("Custom")]
        public bool HasRights
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }
        [Category("Custom")]
        public string FormKey
        {
            get { return this.GetType().FullName; }
        }
        public void LoadForm<T>(T val)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        [Category("Custom")]
        public bool Register
        {
            get { return this.register; }
        }

        private bool mCheckSaveOnClose;
        [Category("Custom")]
        public bool CheckSaveOnClose
        {
            get
            { return this.mCheckSaveOnClose; }
            set
            { this.mCheckSaveOnClose = value; }
        }

        private string mMessageOnClose;
        [Category("Custom")]
        public string MessageOnClose
        {
            get { return mMessageOnClose; }
            set { mMessageOnClose = value; }
        }
        private bool mIsModal = false;
        [Category("Custom")]
        public bool IsModal
        {
            get { return mIsModal; }
            set { mIsModal = value; }
        }
        #endregion
        public virtual void PrepareForSelectDialogStyle()
        {
            this.register = false;
        }

        protected DialogResult ShowError(Exception x)
        {
            return MessageBox.Show(BillingException.ConvertFromException(x).Message, "შეცდომა", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        protected DialogResult ShowInformation(string message)
        {
            return MessageBox.Show(message, "ინფორმაცია", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        protected DialogResult ShowWarning(string message)
        {
            return MessageBox.Show(message, "გაფრთხილება", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        protected DialogResult ShowQuestion(string message)
        {
            return ShowQuestion(message, MessageBoxButtons.YesNo);
        }

        protected DialogResult ShowQuestion(string message, MessageBoxButtons messageBoxButtons)
        {
            return MessageBox.Show(message, "დაადასტურეთ", messageBoxButtons, MessageBoxIcon.Question);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F12)
            {
                KeyboardSwitcher.SwitchKeyboardLayoutBetween("ka-GE", "en-US", true);
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
